﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.Custom;
using DWF.Studio.Share.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace DWF.Studio.Views
{
    /// <summary>
    /// 文件名窗体
    /// </summary>
    public partial class WindowFileName
    {
        public string FileName
        {
            get
            {
                return this.tbxFileName.Text;
            }
        }
        public string Extension
        {
            get;
        }
        public WindowFileName()
        {
            InitializeComponent();
        }
        public WindowFileName(string extension)
            :this()
        {
            InitializeComponent();
            //this.tbExtension.Text = extension;
            this.Title = this.Title + " (" + extension + ")";
            Extension = extension;
        }

        public WindowFileName(string fileName,string extension)
            : this()
        {
            InitializeComponent();
            this.tbxFileName.Text = fileName;
            //this.tbExtension.Text = extension;
            this.Title = this.Title + " (" + extension + ")";
            Extension = extension;
        }

        private void BtnConfirm_Click(object sender, RoutedEventArgs e)
        {
            Color color = (Color)ColorConverter.ConvertFromString("#EA5050");
            SolidColorBrush brush = new SolidColorBrush(color);
            var fileName = this.tbxFileName.Text;
            bool isRegex = Regex.IsMatch(fileName, "[-`~!@#$^&*()=|{}':;',\\\\[\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？]");
            System.IO.Path.GetExtension(fileName);
            if (Extension == StaticResource.XamlExtension)
            {
                if (isRegex)
                {
                    this.label_VerificationTips.Visibility = Visibility.Visible;
                    this.label_VerificationTips.Content = LocalizationProvider.GetLocalizedString("WindowFileName_Label_Verification_NoSpecial");
                    this.img_Error.Visibility = Visibility.Visible;
                    Keyboard.Focus(tbxFileName);
                    tbxFileName.BorderBrush = brush;
                    return;
                }
                else if (String.IsNullOrWhiteSpace(fileName))
                {
                    this.label_VerificationTips.Visibility = Visibility.Visible;
                    this.label_VerificationTips.Content = LocalizationProvider.GetLocalizedString("WindowFileName_Label_Verification_NoEmptyName");
                    this.img_Error.Visibility = Visibility.Visible;
                    Keyboard.Focus(tbxFileName);
                    tbxFileName.BorderBrush = brush;
                    return;
                }
            }
            else {
                if (isRegex)
                {
                    this.label_VerificationTips.Visibility = Visibility.Visible;
                    this.img_Error.Visibility = Visibility.Visible;
                    Keyboard.Focus(tbxFileName);
                    tbxFileName.BorderBrush = brush;
                    return;
                }
                else if (String.IsNullOrWhiteSpace(fileName))
                {
                    this.label_VerificationTips.Visibility = Visibility.Visible;
                    this.label_VerificationTips.Content = LocalizationProvider.GetLocalizedString("WindowFileName_Label_Verification_NoEmptyName");
                    this.img_Error.Visibility = Visibility.Visible;
                    Keyboard.Focus(tbxFileName);
                    tbxFileName.BorderBrush = brush;
                    return;
                }
            }
            this.DialogResult = true;
        }

        private void BtnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void tbxFileName_TextChanged(object sender, TextChangedEventArgs e)
        {
            Color color = (Color)ColorConverter.ConvertFromString("#BCBCBC");
            SolidColorBrush brush = new SolidColorBrush(color);
            tbxFileName.BorderBrush = brush;
            this.label_VerificationTips.Visibility = Visibility.Hidden;
            this.img_Error.Visibility = Visibility.Hidden;
            return;
        }
    }
}
